package InRecord;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.shanlitech.et.SpeechRecorder;
import com.shanlitech.et.SpeechRecorder.SpeechInfo;
import com.shanlitech.et.SpeechRecorder.SpeechRecord;




//@WebServlet("/InRecord")
public class InRecordServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("utf-8");
		
		String speech_id = request.getParameter("speech_id");  
		String group = request.getParameter("group");  
		
		Gson gson = new Gson(); 
		if(group==null || group.isEmpty()){
			 if(speech_id==null || speech_id.isEmpty()){
				send_json(response,"{'ret':error}"); 
				return;
			 }else if(!speech_id.isEmpty()) {
				if(Long.parseLong(speech_id)>0){
					SpeechRecord redata= SpeechRecorder.getInstance().findSpeechRecordById(Long.parseLong(speech_id));
					send_data(response,speech_id,redata);  
					return;
				}else{
					send_json(response,"{'ret':speech_id error}");  
					return;
				} 
			 } 
			 
		}else if (!group.isEmpty()){
			if(speech_id==null || speech_id.isEmpty()){  
				Long integer = Long.parseLong(group);
				if(integer>0){
					List<SpeechInfo> redata=SpeechRecorder.getInstance().findSpeechInfoByGid((integer));
					send_json(response,gson.toJson(redata));  
					return;
				}else{
					send_json(response,"{'ret':group error}");  
					return;
				}
			}else{
				send_json(response,"{'ret':error,'group':"+group+",'speech_id':"+speech_id+"}");  
				return;
			}
		}
		
		send_json(response,"{'ret':error}");  
		return;  
		/*
		if(!group.isEmpty()&&(speech_id==null || speech_id.isEmpty())){  
			Long integer = Long.parseLong(group);
			if(integer>0){
				List<SpeechInfo> redata=SpeechRecorder.getInstance().findSpeechInfoByGid((integer));
				send_json(response,gson.toJson(redata));  
				return;
			}else{
				send_json(response,"{'ret':group error}");  
			}

		}else if((group==null || group.isEmpty())&& !speech_id.isEmpty()) {
			if(Long.parseLong(speech_id)>0){
				SpeechRecord redata= SpeechRecorder.getInstance().findSpeechRecordById(Long.parseLong(speech_id));
				send_data(response,speech_id,redata);  
				return;
			}else{
				send_json(response,"{'ret':speech_id error}");  
			}
		}else{
			send_json(response,"{'ret':error}");  
			return;  
		}	*/
	}
	
	public void send_json(HttpServletResponse response,String data){
        response.setContentType("text/json;charset=utf-8" );  
        response.setCharacterEncoding("utf-8");  

        try{  
        	BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					response.getOutputStream(),"UTF-8"));
            out.write(data);  
            out.flush();  
            out.close();
        }catch(IOException e){  
            e.printStackTrace();  
        } 
	}
	
	public void send_data(HttpServletResponse response,String name,SpeechRecord data){
		
		if(data==null)
			return;
		
		response.setContentType("application/octet-stream;charset=utf-8");
		
		String filenamedisplay = name;
		try {
			filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.addHeader("Content-Disposition", "attachment;filename="
				+ filenamedisplay);
		try {
		OutputStream os= response.getOutputStream();
		for(int i=0;i<data.frames.size();i++)
		{
			os.write(data.frames.get(i), 0, data.frames.get(i).length);
		}
		os.flush();
		os.close();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
		
	}
	
}
